//cocos2d Layer类扩展，实现背景颜色/图片，可见不可见，鼠标点击屏蔽，关闭按钮
//用法：构造类，显示，隐藏，关闭，设置RGBA颜色，设置背景大小，设置是否屏蔽鼠标点击，设置关闭按钮是否可见，
var CanvasLayer = cc.Layer.extend({
	_swallowListener : null,
	
	_background : null,
	
	_closeMenu : null,
	
	node : null,

	maxOpacity : 255,
	
	showDuration : 1,
	
	hideDuration : 1.25,
	
	ctor: function (visible, isSwallow, backgroundSprite) {
		this._super();

		var winSize = cc.director.getWinSize();
		
		visible = visible || false;
		isSwallow = isSwallow || false;
		
		if (backgroundSprite == null){
			backgroundSprite = new cc.Sprite(resource.white1010_png);
			backgroundSprite.attr({
				color : cc.color(0,0,0),
				scaleX : winSize.width * 0.1,
				scaleY : winSize.height * 0.1
			});
		}
		this._background = backgroundSprite;
		this._background.setPosition(0.5 * winSize.width,0.5 * winSize.height);
		this._background.setOpacity(visible ? this.maxOpacity : 0);
		this.addChild(this._background);

		this.node = new cc.Node();
		this.node.setPosition(0,0);
		this.node.setVisible(visible);
		this.addChild(this.node);
		
		this._closeMenu = this._CreateSquareButton("×",0,0,this.onClose);
		this._closeMenu.setPosition(winSize.width - 0.78 * this._closeMenu.width, winSize.height - 0.78 * this._closeMenu.height);
		var menu = cc.Menu.create(this._closeMenu);
		menu.attr({ x : 0, y : 0 });
		this.node.addChild(menu);

		this._swallowListener = null;
		this.setSwallowEvent(isSwallow);
		
		this.ini();
	},

	_CreateSquareButton : function(text,xpos,ypos,callback){
		var button = new cc.MenuItemImage(resource.ctrlbtnNormalZ_png,resource.ctrlbtnDownZ_png, resource.ctrlbtnDisableZ_png, callback, this);
		button.setPosition(xpos, ypos);
		var label = cc.LabelTTF.create(text, "simhei", 28);
		label.setPosition(button.width * 0.5, button.height * 0.5);
		label.setColor(cc.color(0,0,0));
		button.addChild(label);
		return button;
	},
	
	setCloseMenuVisible : function(visible){
		this._closeMenu.setVisible(visible);
	},
	
	setColor : function(color){
		this._background.setColor(cc.color(color.r, color.g, color.b));
		this.maxOpacity = color.a;
		if (this._background.opacity != 0){
			this._background.setOpacity(this.maxOpacity);
		}
				
	},
	
	setSize : function(width, height){
		this._background.setScaleX(width * 0.1);
		this._background.setScaleY(height * 0.1);
	},
	
	show : function(isSwallow){
		this.setSwallowEvent(isSwallow);
		
		this._background.stopAllActions();
		this._background.runAction(cc.fadeTo(this.showDuration - 0.1, this.maxOpacity));
		this.node.stopAllActions();
		this.node.runAction(cc.sequence(cc.delayTime(this.showDuration - 0.1), cc.show()));
	},
	
	hide : function(isSwallow){
		this.setSwallowEvent(isSwallow);
		
		this.node.setVisible(false);
		this._background.stopAllActions();
		this._background.runAction(cc.fadeOut(this.hideDuration - 0.1));
	},
	
	setSwallowEvent : function(isSwallow){
		if (isSwallow == null)
			return;
		
		if (isSwallow){
			if (this._swallowListener != null)
				return;
			
			this._swallowListener = cc.EventListener.create({
				event: cc.EventListener.TOUCH_ONE_BY_ONE, //TOUCH_ONE_BY_ONE 为单次触摸事件监听器
				swallowTouches: true,
				onTouchBegan:function (touch,event) {
					return true;
				}
			});
			cc.eventManager.addListener(this._swallowListener, this);
		}else{
			if (this._swallowListener == null)
				return;
			
			cc.eventManager.removeListener(this._swallowListener);
			this._swallowListener = null;
		}
	},
	
	ini : function(){
	},
	
	destroy : function(){
	},
	
	onClose : function(sender){
		this.hide();
		this.runAction(	cc.sequence(
				cc.callFunc(this.destroy),
				cc.delayTime(this.hideDuration),
				cc.removeSelf()));
	},
});